#include "Line.h"

// make sure pointer is 0
Line::Line() { m_tex = 0; }

// make sure safe release is called
Line::~Line() { release(); }

// safetly release all stored data
void Line::release()
{
}

// setup texture and start and end points
void Line::initialize(Texture * a_tex, V2DF start, V2DF end)
{
	// save texture reference
	m_tex = a_tex;
	// set start and end points
	m_start = start;
	m_end = end;

	calculate();
}

// set start point and recalculate length/angle
void Line::setStart(V2DF start)
{
	// set start
	m_start = start;
	calculate();
}

// set end point and recalculate length/angle
void Line::setEnd(V2DF end)
{
	// end points
	m_end = end;
	calculate();
}

// determine how many times the texture must be drawn and if ....
// a portion of the texture must be drawn
void Line::calculate()
{
	// save sprite height and width
	// if the texture is a sprite sheet or animation get the individual height and widht
	if(m_tex->getType() == SHEET || m_tex->getType() == ANIMATION)
	{
		// a sprite sheet pointer will work fine for either
		SpriteSheet * temp = (SpriteSheet*)m_tex;
		m_spriteHeight = temp->SpriteHeight();
		m_spriteWidth = temp->SpriteWidth();
	}
	// otherwise get the image height and width
	else
	{
		m_spriteHeight = m_tex->imageHeight();
		m_spriteWidth = m_tex->imageWidth();
	}

	// calculate length
	V2DF difference = m_end.difference(m_start);
	m_length = difference.length();
	// calculate angle
	m_angle = atan2f(difference.y,difference.x)*(180.0f/V2D_PI);

	// determine the number of times the image must be drawn
	m_numSprites = (int)m_length / m_spriteHeight;

	// now check if a portion of a sprite must be drawn
	if( (int)m_length % m_spriteHeight != 0 )
	{
		float remainder = m_spriteHeight - ( m_length - (m_spriteHeight * m_numSprites) ) ;
		RECT temp;
		// cut off the top of the texture
		temp.top = (int)remainder; 
		// leave the rest the same
		temp.left = 0;
		temp.right = m_spriteWidth;
		temp.bottom = m_spriteHeight;
		m_lastSprite = temp;
	}
}

// render the line from start point to end point
void Line::render()
{
//	float angleRad = (V2D_PI / 180.0f) * wanderAngle;
//	steeringForce = V2DF( cos(angleRad), sin(angleRad) ).product( 10 );

	// calculate half of the image length
	float halfImage = (float)m_spriteHeight / 2.0f;
	// convert angle to radians
	float angleRad = (V2D_PI / 180.0f) * m_angle;
	// create vector based on half image length and angle in radians
	V2DF halfImageLength( cos(angleRad), sin(angleRad) );
	// get vector of full image length
	V2DF imageLength = halfImageLength.product( m_spriteHeight );
	halfImageLength.multiply( halfImage );
	// set up draw position
	V2DF drawPos;
	if(m_numSprites > 0)
		drawPos = m_start.sum( halfImageLength );
	else
		drawPos = m_end;
	// reset textures view rect
	m_tex->resetViewRect();
	// draw each sprite
	for(int i = 0; i < m_numSprites; i++)
	{
		// add image length
		if(i > 0)
			drawPos.add(imageLength);
		m_tex->draw(drawPos, m_angle+90.0f,1.0f);
	}

	// now check if a portion of a sprite must be drawn
	if( (int)m_length % m_spriteHeight != 0 )
	{
		m_tex->setViewRect(m_lastSprite);
		if(m_numSprites > 0)
		{
			// determine mid point between end of the last full texture drawn and the end of the line
			drawPos.add(halfImageLength);
			drawPos = drawPos.difference(m_end).product(0.5f).sum(m_end);
		}
		else
		{
			// the length of the line is less than
			// determine the mid point between the start and end points of the line
			drawPos = m_start.difference(m_end).product(0.5f).sum(m_end);
		}
		// draw the final partial sprite
		m_tex->draw(drawPos,m_angle+90.0f,1.0f);
	}
}

// used if the texture is an animation
void Line::update(float dT)
{
	if( m_tex->getType() == ANIMATION )
	{
		// update the texture
		Animation * temp = (Animation*)m_tex;
		temp->update(dT);
	}
}